package com.hw.hcit.feature;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

public class HeadsetWireControlTest extends JTestItem implements
		OnClickListener {
	private static final String LOG_TAG = "HeadsetWireControlTest ";
	private Context mContext;
	private ImageView mHeadsetIn;
	private ImageView mHeadsetOut;
	private ImageView mHeadsetCtrlPressed;
	private boolean mHeadsetExist = false;
	private boolean bHeadsetIn = false;
	private boolean bHeadsetOut = false;
	private boolean bHeadsetCtrlPressed = false;
	private boolean mPowerClicked;
	private Button mButton1,mButton2;

	public HeadsetWireControlTest(JCtrlCallback cb) {
		super(cb);
	}

	public HeadsetWireControlTest() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.headset_wire_control_test,
				container, false);
		mHeadsetIn = (ImageView) rootView.findViewById(R.id.headset_in);
		mHeadsetCtrlPressed = (ImageView) rootView
				.findViewById(R.id.headset_control_pressed);
		mHeadsetOut = (ImageView) rootView.findViewById(R.id.headset_out);
		mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(20, 'P');
					} else {
						rewriteOneByteOfNv2499(51, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(20, 'F');
					} else {
						rewriteOneByteOfNv2499(51, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = activity;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mPowerClicked = false;
		bHeadsetIn = false;
		bHeadsetOut = false;
		bHeadsetCtrlPressed = false;
		mButton1.setEnabled(false);
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_HEADSET_PLUG);
		mContext.registerReceiver(mReceiver, filter);
		if (mHeadsetExist) {
			mHeadsetIn
					.setImageResource(R.drawable.wire_control_headset_in_true);
			bHeadsetIn = true;
		} else {
			// do nothing
		}

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mContext.unregisterReceiver(mReceiver);
		bHeadsetIn = false;
		bHeadsetOut = false;
		bHeadsetCtrlPressed = false;
		mHeadsetIn.setImageResource(R.drawable.wire_control_headset_in_false);
		mHeadsetOut.setImageResource(R.drawable.wire_control_headset_out_false);
		mHeadsetCtrlPressed
				.setImageResource(R.drawable.wire_control_pressed_false);
	}

	@Override
	public boolean getResult() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getResultInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startTest() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stopTest() {
		// TODO Auto-generated method stub

	}

	public void refresh() {

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
				Log.d(LOG_TAG, LOG_TAG + "ACTION_HEADSET_PLUG");
				if (intent.getIntExtra("state", 0) == 0) {
					mHeadsetExist = false;
					if (bHeadsetIn == true) {
						mHeadsetOut
								.setImageResource(R.drawable.wire_control_headset_out_true);
						bHeadsetOut = true;
					}
				} else {
					mHeadsetExist = true;
					mHeadsetIn
							.setImageResource(R.drawable.wire_control_headset_in_true);
					bHeadsetIn = true;
				}
				if (bHeadsetCtrlPressed && bHeadsetIn && bHeadsetOut) {
					mButton1.setEnabled(true);
				}
			}
		}
	};

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d(LOG_TAG,
				LOG_TAG + "dispatchKeyEvent, KEYCODE = " + event.getKeyCode());
		if (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) {
			mHeadsetCtrlPressed
					.setImageResource(R.drawable.wire_control_pressed_true);
			bHeadsetCtrlPressed = true;
			if (bHeadsetCtrlPressed && bHeadsetIn && bHeadsetOut) {
				mButton1.setEnabled(true);
			}
			return true;
		}
		return true;

	}

}
